//
//  ScanViewController.m
//  VinScanner
//
//  Created by Le Quang Vinh on 11/29/12.
//  Copyright (c) 2012 Le Quang Vinh. All rights reserved.
//

#import "ScanViewController.h"
#import "CustomOverlayView.h"

@interface ScanViewController () {
    CustomOverlayView *overlay;
}

@end

@implementation ScanViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:kNameScannerView];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Action
#pragma mark
- (IBAction)onScanVINs:(id)sender
{
    overlay = [[CustomOverlayView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    overlay.delegate = self;
    
    _imagePickerController = [[UIImagePickerController alloc] init];
    _imagePickerController.delegate = self;
    _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    _imagePickerController.showsCameraControls = YES;
    _imagePickerController.cameraViewTransform = CGAffineTransformScale(_imagePickerController.cameraViewTransform, 1, 1);
    _imagePickerController.cameraOverlayView = overlay;
    
    [self presentModalViewController:_imagePickerController animated:YES];
   
}

#pragma mark - Delegate capture
#pragma mark
- (void)takePicture
{
    [_imagePickerController takePicture];
}

- (void)cancelCapture
{
    [_imagePickerController dismissModalViewControllerAnimated:YES];
}

#pragma mark - Delegate UIImagePickerController
#pragma mark
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [_imagePickerController dismissModalViewControllerAnimated:YES];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    CGSize size = CGSizeMake(320, 427);
    image = [Utilities imageByScalingToSize:size WithImage:image];
    PreviewImageViewController *previewController = [[PreviewImageViewController alloc] initWithNibName:@"PreviewImageViewController" bundle:nil];
    previewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:previewController animated:YES];
    previewController.pictureImg = image;
    [previewController release];
}

@end
